<?php

namespace App\Kuafu\Order\Controller\Internal;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Common\Enum\CodeEnum;
use App\Kuafu\Order\Enum\OrderMerchantEnum;
use App\Kuafu\Order\Service\OrderMerchantUseService;
use App\Kuafu\Order\Service\OrderService;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Service\OpenShopService;
use App\Kuafu\Shop\Service\ShopService;
use App\Kuafu\Shop\Validate\ShopValidate;
use App\Kuafu\WaiMai\Channel\ElmWaiMai;
use App\Kuafu\WaiMai\Model\WaiMaiShopMappingModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class OrderController extends BaseController
{
    public function orderDetailList(Request $request)
    {
        $shopId            = $request->get('shopId');
        $completeTimeStart = $request->get('completeTimeStart', 0);
        $completeTimeEnd   = $request->get('completeTimeEnd', 0);
        $pageSize          = $request->get('pageSize', 10);
        $pageNum           = $request->get('pageNum', 1);

        $field = [
            'serialNum',
            'orderId',
            'originalPrice',
            'shouldPrice',
            'payPrice',
            'discountPrice',
            'payChannel',
            'completeTime',
            'refundStatus',
            'orderStatus',
            'createdBy',
            'refundMoney',
            'createdAt',
            'realPayPrice',
            'realDiscountPrice',
            'invoiceStatus',
            'invoiceCanCancelCount'
        ];
        $where = [
            'completeTimeStart' => (int)$completeTimeStart,
            'completeTimeEnd'   => (int)$completeTimeEnd,
        ];
        $lists = OrderService::getOrderDetailList($shopId, $where, $field, (int)$pageSize, (int)$pageNum);

        return $this->success($lists);
    }

    public function originalPrice(Request $request)
    {
        $shopId      = (int)$request->get('shopId');
        $showTimeTab = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_DEFINE);
        $showStartAt = (int)$request->get('showStartAt');
        $showEndAt   = (int)$request->get('showEndAt');


        $data = (new OrderMerchantUseService([$shopId]))
            ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
            ->originalPrice();

        return $this->success($data);
    }

    public function waiMaiToken(Request $request)
    {
        $mapping = WaiMaiShopMappingModel::query()
                                         ->where('sourceType', \App\Kuafu\Order\Enum\CodeEnum::SOURCE_TYPE_ELM)
                                         ->first();

        $data = (array)ElmWaiMai::getIns(\App\Kuafu\Order\Enum\CodeEnum::SOURCE_TYPE_ELM, $mapping->shopId,
            $mapping->thirdShopId)->getToken();

        return $this->success($data);
    }
}
